//
//  LeftVC.m
//  ImGuider
//
//  Created by llt on 2017/6/22.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "LeftVC.h"
#import "LoginVC.h"
#import "UIView+Gradient.h"
#import "AccountCell.h"

@interface LeftVC ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *useNameLabel;

@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation LeftVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self reloaUserInfodPage];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloaUserInfodPage) name:kRefreshUserNotif object:nil];
}

- (void)setupUI {
    
    self.headImageView.layer.borderColor = kContentColor.CGColor;
    self.headImageView.layer.borderWidth = 0.5f;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view gradientWithColorArray:@[kThemeLightColor,kThemeDarkColor] frame:CGRectMake(0, 0, SCREEN_WIDTH * 4 / 5.0, SCREEN_HEIGHT) isLeftToRight:NO];
    
    self.loginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.loginBtn.layer.borderWidth = 1.0f;
    self.loginBtn.layer.cornerRadius = 5.0f;
    
    [self.loginBtn setTitle:MYLocalizedString(@"ACCOUNT_LOGIN", nil) forState:UIControlStateNormal];
    
    if ([UserManager sharedManager].token) {
        
        @weakify(self);
        [NetAPIManager getUserInfoWithBlock:^(NSString *result, NSError *error) {
            
            @strongify(self);
            [self reloaUserInfodPage];
        }];
        
    }
}

- (void)resetDataSource {
    
    if ([[UserManager sharedManager].user.role integerValue] != UserRolesTypeLow && [UserManager sharedManager].token) {
        
        self.dataSource = @[@{@"name":@"ACCOUNT_ORDER",@"image":@"订单",@"class":@"AccountOrderListVC"},
                            @{@"name":@"ACCOUNT_DOWNLOAD",@"image":@"下载",@"class":@"AccountDownloadVC"},
                            @{@"name":@"ACCOUNT_MY_QUESTION",@"image":@"提问",@"class":@"AccountMyQuestion"},
                            @{@"name":@"ACCOUNT_FLLOW",@"image":@"关注",@"class":@"AccountCollectVC"},
                            @{@"name":@"ACCOUNT_VOUCHER",@"image":@"优惠券",@"class":@"AccountTicketVC"},
                            @{@"name":@"ACCOUNT_SETTING",@"image":@"设置",@"class":@"AccountSettingVC"}
                            ];
        
        return;
        
    }
    self.dataSource = @[@{@"name":@"ACCOUNT_ORDER",@"image":@"订单",@"class":@"AccountOrderListVC"},
                        @{@"name":@"ACCOUNT_DOWNLOAD",@"image":@"下载",@"class":@"AccountDownloadVC"},
                        @{@"name":@"ACCOUNT_MY_QUESTION",@"image":@"提问",@"class":@"AccountMyQuestion"},
                        @{@"name":@"ACCOUNT_FLLOW",@"image":@"关注",@"class":@"AccountCollectVC"},
                        @{@"name":@"ACCOUNT_SETTING",@"image":@"设置",@"class":@"AccountSettingVC"}
                        ];
}

- (IBAction)modifyUserinfoAction:(id)sender {
    
    UIViewController *accVC = [IGUtils loadViewController:@"AccountUserinfoVC" storyboard:@"Account"];
    
    [self hideLeftViewAnimated:nil];
    
    [(UINavigationController *)self.sideMenuController.rootViewController pushViewController:accVC animated:YES];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)reloaUserInfodPage {
    
    [self resetDataSource];
    
    CGRect headFrame = self.headView.frame;
    
    if ([UserManager sharedManager].token) {
        
        headFrame.size.height = 100;
        self.headView.hidden = NO;
        self.loginBtn.hidden = YES;
        self.useNameLabel.text = [UserManager sharedManager].user.realname;
        [self.headImageView setImageURL:[NSURL URLWithString:[UserManager sharedManager].user.headimg] placeholderImage:[UIImage imageNamed:@"default"]];
        
    } else {
        
        headFrame.size.height = 24;
        self.headView.hidden = YES;
        self.loginBtn.hidden = NO;
    }
    
    self.headView.frame = headFrame;
    
    if (!kiOS9Later) {
        [self.tableView setTableHeaderView:self.headView];
    }
    
    [self.tableView reloadData];
}



#pragma mark-UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSDictionary *dict = self.dataSource[indexPath.row];
    
    [cell configWithDict:dict];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row < (self.dataSource.count - 1) && ![UserManager sharedManager].token) { // 我的相关
        
        [self loginAction:nil];
        
        return;
    }
    
    
    NSDictionary *dict = self.dataSource[indexPath.row];
    
    UIViewController *accVC = [IGUtils loadViewController:dict[@"class"] storyboard:@"Account"];
    
    [self hideLeftViewAnimated:nil];
    
    [(UINavigationController *)self.sideMenuController.rootViewController pushViewController:accVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 60;
    }
    
    return SCREEN_HEIGHT - 60 * 5;
}

- (IBAction)loginAction:(id)sender {
    
    LoginVC *loginVC = [LoginVC new];
    loginVC.delegate = self;
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)loginSuccessCallback {
    
    [self reloaUserInfodPage];
    
    if ([UserManager sharedManager].token) {
        
        [NetAPIManager getUserInfoWithBlock:^(NSString *result, NSError *error) {
            
            [self reloaUserInfodPage];
        }];
        
    }
}

@end
